home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1442 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.7 KB

  1. Path: rap.SanDiegoCA.ATTGIS.COM!es013!jbc
  2. From: jbc@ElSegundoCA.ATTGIS.COM (Jim Chapman)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: deque container - how to implement?
  5. Date: 11 Jan 1996 01:17:21 GMT
  6. Organization: AT&T Global Information Solutions
  7. Distribution: world
  8. Message-ID: <4d1of1$kj7@rap.SanDiegoCA.ATTGIS.COM>
  9. References: <4cvepv$5rn@news.xmission.com>
  10. Reply-To: jbc@ElSegundoCA.ATTGIS.COM
  11. NNTP-Posting-Host: es013.elsegundoca.attgis.com
  12.  
  13. In article 5rn@news.xmission.com, tknarr@xmission.com  ( Todd Knarr ) writes:
  14. > In <4ct0c3$9gg@news.bridge.net>, David Byrden <100101.2547@compuserve.com> writes:
  15. > >I have hardly studied deque and have never seen an implementation, but I 
  16. > >will bet that it works like this; it consists of a contigous block of 
  17. > >elements in the MIDDLE of a larger block of free space. Adding elements 
  18. > >is fast because there is free space at each end. When it expands to bump 
  19. > >either end of its free space, it allocates a larger block of memory from 
  20. > >the heap and copies all its contents into the MIDDLE of the new block. 
  21. > >Altering things in the middle is slow because the elements must remian 
  22. > >contiguous, so up to half of the data will need to move.
  23. > They can be implemented that way ( based on an array of elements ), but
  24. > it's unusual because of the physical shuffling needed. More often they
  25. > are implemented using pointers. For a deque of objects of class T, you
  26. > make a node type like so ( for a fully non-intrusive list ):
  27. > struct Node
  28. > {
  29. >     Node *pNext, *pPrev;
  30. >     T *pElement;
  31. > };
  32. > You start out with head and tail pointers set to the first and last
  33. > nodes in the list. Inserting an item simply involves making a new node
  34. > for it, pointing that node's pNext and pPrev pointers to the nodes
  35. > just before and after it in the list, then pointing those node's pNext
  36. > and pPrev pointers to the new node as appropriate. As sample code,
  37. > assuming pInsertPoint points to the node I want to insert after and
  38. > pNewNode points to the node to insert with pElement filled in, I would
  39. > do:
  40. > pNewNode->pPrev = pInsertPoint;
  41. > pNewNode->pNext = pInsertPoint->pNext;
  42. > pInsertPoint->pNext->pPrev = pNewNode;
  43. > pInsertPoint->pNext = pNewNode;
  44. > Removing an element simply involves finding the node and cutting it out
  45. > of the chain by pointing the pNext and pPrev pointers of the nodes before
  46. > and after it to each other.
  47. > Implementation notes:
  48. > 1) The idea of a Node structure seperate from the element is solely to
  49. > allow building lists of things without needing any fields in the things
  50. > to be put on the list. You can also put the next and previous pointers
  51. > in the actual items and avoid allocating and deallocating nodes all the
  52. > time.
  53. > 2) Most implementations that use Node structures use two dummy nodes to
  54. > anchor the head and tail of the list. They usually have NULL pElement
  55. > pointers to distinguish them from real nodes. This eliminates special-case
  56. > code to deal with an empty list or the ends of the list.
  57. > --
  58. > Todd Knarr : tknarr@xmission.com      |  finger for PGP public key
  59. >                                       |  Member, USENET Cabal
  60. > Seriously, I don't want to die just yet.  I don't care how
  61. > good-looking they are, I! don't! want! to! die!"
  62. >                                         -- Megazone ( UF1 )
  63.  
  64. In STL, deque<T> is supposed to support random access in constant time, so
  65. a linked list implemention wouldn't do.  The first poster was closer to
  66. the mark. 
  67.  
  68. ---
  69.  ====  AT&T        | Jim Chapman             |
  70. =--=== Global      | Multimedia Projects     | jbc@ElSegundoCA.ATTGIS.COM
  71. =--=== Information | 100 N. Sepulveda Blvd.  |   Voice: (310) 524-6747          
  72.  ====  Solutions   | El Segundo, CA 90245    |   FAX:   (310) 524-5515
  73.  
  74.